﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Extensil.Common.CategoryHandler.Discovery;
using Extensil.Common.CategoryHandler;
using Extensil.Common;
using Extensil.Processor.Extensibility;

namespace Extensil.Category.Discovery
{
    public partial class ProbeConfiguration :Form
    {
        Probe _probe;
        public ProbeConfiguration()
            : this(null)
        {
        }
        public ProbeConfiguration(Probe prb)
        {
            InitializeComponent();
            _probe = prb;
            LoadControls();
        }
        void LoadControls()
        {
            LoadHandlers();
            if(_probe != null)
            {
                txtName.Text = _probe.Name;
                foreach(ComboPropertyParam cpp in cbHandler.Items)
                {
                    if(cpp.Key == _probe.Handler)
                    {
                        cbHandler.SelectedIndex = cbHandler.Items.IndexOf(cpp);
                        break;
                    }
                }
                SetProbeParam();
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                _probe = new Probe();
                if(String.IsNullOrEmpty(txtName.Text.Trim()))
                    throw new Exception("Name can not be empty for a probe.");
                _probe.Name = txtName.Text;
                ProbeParamList pars = new ProbeParamList();
                foreach(DataGridViewRow dr in dgvHandlerList.Rows)
                {
                    ProbeParam pp = new ProbeParam(dr.Cells["Name"].Value.ToString());
                    pp.Value = (dr.Cells["Value"].Value == null ? "" : dr.Cells["Value"].Value.ToString());
                    pars.Add(pp);
                }
                BaseProbeHandler cd = PluginAssembly.GetHandler<BaseProbeHandler>(cbHandler.SelectedValue.ToString());
                string message;
                if(!cd.ValidateParam(pars, out message))
                    throw new Exception(message);
                _probe.Handler = ((ComboPropertyParam)cbHandler.SelectedItem).Key;
                _probe.Clear();

                _probe.AddRange(pars);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch(Exception exc)
            {
                AlertBox.ShowDialog(exc);
            }
        }
        public Probe Probe
        {
            get
            {
                return _probe;
            }
        }

        private void cbHandler_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetProbeParam();
        }
        void LoadHandlers()
        {
            List<ComboPropertyParam> lstHand = new List<ComboPropertyParam>();
            foreach(PluginHandler hnd in CategorySection.GetConfig().ProbeHandlers)
            {
                lstHand.Add(new ComboPropertyParam(hnd.Name, hnd.Path));
            }
            cbHandler.DisplayMember = "Key";
            cbHandler.ValueMember = "Value";
            cbHandler.DataSource = lstHand;
        }
        void SetProbeParam()
        {
            BaseProbeHandler cd = PluginAssembly.GetHandler<BaseProbeHandler>(cbHandler.SelectedValue.ToString());
            if(_probe != null)
            {
                for(int i = 0; i < cd.Parameters.Count; i++)
                {
                    if(_probe[cd.Parameters[i].Name] != null)
                        cd.Parameters[i].Value = _probe[cd.Parameters[i].Name].Value;
                }
            }
            dgvHandlerList.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dgvHandlerList_DataBindingComplete);
            dgvHandlerList.DataSource = cd.Parameters;
        }

        void dgvHandlerList_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            foreach(DataGridViewColumn dc in dgvHandlerList.Columns)
            {
                dc.Visible = false;
            }
            dgvHandlerList.Columns["Name"].Visible = true;
            dgvHandlerList.Columns["Name"].ReadOnly = true;
            dgvHandlerList.Columns["Name"].DefaultCellStyle.BackColor = Color.WhiteSmoke;
            dgvHandlerList.Columns["Value"].Visible = true;
            dgvHandlerList.Columns["Value"].ReadOnly = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
